Bound file matches in useSuggestionEngine and eliminate fuzzy match allocations - #1226
Bound file matches in useSuggestionEngine and eliminate fuzzy match allocations#1226nordicnode wants to merge 1 commit into
Conversation
|
Nice, tightly scoped change. Capping The new tests in One thing worth double-checking before this lands: the cap is applied after Also worth a quick grep to confirm Overall this looks like the right fix in the right place with tests, small enough to review confidently. |
Bound file matches in useSuggestionEngine and eliminate fuzzy match allocations
Summary
• In
cli/src/hooks/use-suggestion-engine.ts, bounded file suggestions to the top 100 ranked candidates and eliminated redundant intermediate array allocations in highlight generation and fuzzy matching.• Previously, in projects with thousands of files, typing
@to trigger a file mention causedfilterFileMatchesto return all matching files across the entire project tree.• Downstream,
fileSuggestionItemsmapped over every returned match, executing multiple string operations (getFileName,filePath.lastIndexOf,includes) and allocating{ id, label, labelHighlightIndices, description, descriptionHighlightIndices }objects and arrays for thousands of off-screen files on every single keystroke.• Because the suggestion pop-up menu only displays 5 visible items at a time (
maxVisible={5}), computing and rendering thousands of React suggestion objects created memory spikes, garbage collection pauses, and keystroke lag.• Bounded
filterFileMatchesto return the top 100 best-scored matches (following the pattern established inchat-history-screen.tsx).• Optimized
createHighlightIndicesto populate index arrays in a single direct loop, eliminating the intermediate array spread[...range(start, end)].• Replaced
indices.filter(...).lengthinfuzzyMatchwith an in-place counter loop, avoiding throwaway array allocations for every candidate match.• Added unit test coverage in
cli/src/hooks/__tests__/use-suggestion-engine-mention.test.tsverifying that matches are capped to 100 when exceeding the threshold and preserved when below it (102/102 tests pass).Verification & Benchmark Results
1. Benchmark
@queries in large repositories.2. Test Suite & Hygiene
bun test --config=/dev/null --preload ../sdk/test/setup-env.ts src/hooks/__tests__/use-suggestion-engine*passed 102/102 tests (0 fail).bun run --cwd cli typecheckpassed with 0 errors.check-pr-hygiene.ts).